/*
 * This file is part of aion-lightning <aion-lightning.org>
 *
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.player;

import com.aionemu.commons.network.util.ThreadPoolManager;
import com.aionengine.gameserver.configs.main.SecurityConfig;
import com.aionengine.gameserver.model.ChatType;
import com.aionengine.gameserver.model.Race;
import com.aionengine.gameserver.model.TaskId;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.PacketSendUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Source
 */
public class PlayerChatService {

    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger("CHAT_LOG");

    /**
     * This method will control players msg
     *
     * @param player
     */
    public static boolean isFlooding(final Player player) {
        player.setLastMessageTime();

        if (player.floodMsgCount() > SecurityConfig.FLOOD_MSG) {
            player.setGagged(true);
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_FLOODING);
            player.getController().cancelTask(TaskId.GAG);
            player.getController().addTask(TaskId.GAG, ThreadPoolManager.getInstance().schedule(new Runnable() {

                @Override
                public void run() {
                    player.setGagged(false);
                    PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_CAN_CHAT_NOW);
                }

            }, 2 * 60000L));

            return true;
        }

        return false;
    }

    public static void chatLogging(Player player, ChatType type, String message) {
        switch (type) {
            case GROUP:
                log.info(String.format("[MESSAGE] - GROUP <%d>: [%s]> %s", player.getCurrentTeamId(), player.getName(),
                        message));
                break;
            case ALLIANCE:
                log.info(String.format("[MESSAGE] - ALLIANCE <%d>: [%s]> %s", player.getCurrentTeamId(), player.getName(),
                        message));
                break;
            case GROUP_LEADER:
                log.info(String.format("[MESSAGE] - LEADER_ALERT: [%s]> %s", player.getName(), message));
                break;
            case LEGION:
                log.info(String.format("[MESSAGE] - LEGION <%s>: [%s]> %s", player.getLegion().getLegionName(),
                        player.getName(), message));
                break;
            case LEAGUE:
            case LEAGUE_ALERT:
                log.info(String.format("[MESSAGE] - LEAGUE <%s>: [%s]> %s", player.getCurrentTeamId(), player.getName(),
                        message));
                break;
            case NORMAL:
            case SHOUT:
                if (player.getRace() == Race.ASMODIANS)
                    log.info(String.format("[MESSAGE] - ALL (ASMO): [%s]> %s", player.getName(), message));
                else
                    log.info(String.format("[MESSAGE] - ALL (ELYOS): [%s]> %s", player.getName(), message));
                break;
            default:
                if (player.isGM())
                    log.info(String.format("[MESSAGE] - ALL (GM): [%s]> %s", player.getName(), message));
                break;
        }
    }

}